package com.enlorenz.common.bank;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.enlorenz.common.excel.input.ExcelDataImporter;
import com.enlorenz.common.excel.input.IDataInputIterator;
import com.enlorenz.common.util.FileUtils;

/**
 * 卡片工具
 * @author Karma
 */
public class BankCardBinUtils {
	private static Map<String,BankCardInfo> cardMapping;
	public static final String ENCODING="utf-8";
	public static final String FILE_POSP_CARDBINTB="posp_cardbintb.xls";
	public static final String FILE_BANK_UNI_NO="bank_uni_no.properties";
	/**
	 * 初始化卡牌映射集合
	 */
	public static void initCardMapping(){
		initCardMapping(ENCODING);
	}
	
	/**
	 * 初始化卡牌映射集合
	 * @param encoding 字符编码
	 */
	public static void initCardMapping(String encoding){
		ExcelDataImporter excelParser=new ExcelDataImporter(BankCardBinUtils.class,FILE_POSP_CARDBINTB);
		excelParser.setEncoding(encoding);
		
		Properties properties=FileUtils.loadProperties(BankCardBinUtils.class,FILE_BANK_UNI_NO,encoding);
		
		initCardMapping(properties, excelParser);
	}
	
	
	/**
	 * 初始化卡牌映射集合
	 * @param cardIs 卡牌文件流
	 * @param bankIs 银行文件流
	 * @param encoding 字符编码
	 */
	public static void initCardMapping(InputStream cardIs,InputStream bankIs,String encoding){
		ExcelDataImporter excelParser=new ExcelDataImporter(FILE_POSP_CARDBINTB,cardIs);
		excelParser.setEncoding(encoding);
		
		Properties properties=FileUtils.loadProperties(bankIs,encoding);
		initCardMapping(properties, excelParser);
	}
	
	/**
	 * 是否是信用卡
	 * @param cardNo 卡号
	 * @return 结果
	 */
	public static boolean isCreditCard(String cardNo) throws UnableIdentifyBankCardException{
		if(null == cardMapping){
			initCardMapping();
		}
		BankCardInfo bci=getBankCardInfo(cardNo);
		if(BankCardTypeEnum.CARDTYPE_CREDITED.isEq(bci.getCardType())){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 是否为借记卡
	 * @param cardNo 卡号
	 * @return 结果
	 */
	public static boolean isDebitCard(String cardNo) throws UnableIdentifyBankCardException{
		if(null == cardMapping){
			initCardMapping();
		}
		BankCardInfo bci=getBankCardInfo(cardNo);
		if(BankCardTypeEnum.CARDTYPE_DEBIT.isEq(bci.getCardType())){
			return true;
		}else{
			return false;
		}
	}
	
	
	/**
	 * 通过卡号获取卡片信息
	 * @param cardNo 卡片号码
	 * @return 卡片信息对象
	 */
	public static BankCardInfo getBankCardInfo(String cardNo) throws UnableIdentifyBankCardException{
		if(null == cardMapping){
			initCardMapping();
		}
		String prefix=cardNo.substring(0,6);
		BankCardInfo bci=cardMapping.get(prefix);
		if(null == bci){
			UnableIdentifyBankCardException ubcexp=new UnableIdentifyBankCardException();
			ubcexp.setBankCardNo(cardNo);
			throw ubcexp;
		}
		return bci;
	}
	
	
	public static void initCardMapping(final Properties properties,final ExcelDataImporter excelParser){
		cardMapping=new HashMap<String, BankCardInfo>();
		excelParser.parse(new IDataInputIterator() {
			
			@Override
			public void sheet(int sheetNum, String sheetName) {
			
			}
			
			@Override
			public void row(int rowNum, List<String> colLost) {
				if(rowNum<4){
					return;
				}
				
				String card=colLost.get(0);
				String [] infoArr=card.split("\\(|\\)");
				
				BankCardInfo bci=new BankCardInfo();
				bci.setBankCode(infoArr[1].trim());
				bci.setBankName(infoArr[0].trim());

				bci.setUniCode(properties.getProperty(bci.getBankName()));
				bci.setCardName(colLost.get(1));
				bci.setCardType(BankCardTypeEnum.getValueByDesp(colLost.get(15)));
				bci.setPrefix(colLost.get(13));
				cardMapping.put(bci.getPrefix(), bci);
			}
		});
	}
	
	
	
}
